package com.zhq.apputil.utils;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * TimeUtils
 * Created by Yancy on 2015/12/2.
 */
public class TimeUtils {

    private final static String TAG = "TimeUtils";

    public final static String PATTERN = "yyyy-MM-dd";

    public final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public final static String DATE_SPECAIL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    public static String timeFormat(long timeMillis, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return simpleDateFormat.format(new Date(timeMillis));
    }


    public static String formatPhotoDate(long time) {
        return timeFormat(time, PATTERN);
    }

    public static String formatPhotoDate(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            long time = file.lastModified();
            return formatPhotoDate(time);
        }
        return "1970-01-01";
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getDate() {
        Date date = new Date();// 获取当前日期对象
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置格式
        return format.format(date);
    }

    public static String getDate(String formatStr) {
        Date date = new Date();// 获取当前日期对象
        SimpleDateFormat format = new SimpleDateFormat(formatStr);// 设置格式
        return format.format(date);
    }

    public static String getDate(String formatStr, long times) {
        Date date = new Date(times);// 获取当前日期对象
        SimpleDateFormat format = new SimpleDateFormat(formatStr);// 设置格式
        return format.format(date);
    }


    /***
     * 时间特殊类型转换 2019-03-22T09:11:52.000+0000
     * @return
     */
    public static String getFormatTime(String dateStr) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(dateStr);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            Date date1 = df1.parse(date.toString());
            DateFormat df2 = new SimpleDateFormat(TIME_FORMAT);
            return df2.format(date1);
        } catch (Exception e) {
            return "";
        }
    }


    public static String stringToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String dateTime = "";
        try {
            dateTime = sdf.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    public static Calendar stringToDate(String dateStr, String dateFormat) {
        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            Date date = formatter.parse(dateStr);
            calendar.setTime(date);
            return calendar;
        } catch (ParseException e) {
            return null;
        }
    }


    /***
     * 根据当前时间戳 获取当前的时间
     * @param time
     * @param formatStr
     * @return
     */
    public static String getHourTime(String time, String formatStr) {
        Calendar calendar = stringToDate(time, formatStr);
        StringBuffer stringBuffer = new StringBuffer();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (hour < 9) {
            stringBuffer.append("0");
        }
        stringBuffer.append(hour);
        stringBuffer.append(":");
        if (minute < 9) {
            stringBuffer.append("0");
        }
        stringBuffer.append(minute);
        stringBuffer.append(":");
        if (second < 9) {
            stringBuffer.append("0");
        }
        stringBuffer.append(second);
        return stringBuffer.toString();
    }

}